﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.Activities
    <Designer(GetType(CodeDesigner), GetType(IDesigner)), SRCategory("Standard"), ActivityValidator(GetType(CodeActivityValidator)), ToolboxItem(GetType(ActivityToolboxItem)), ToolboxBitmap(GetType(CodeActivity), "Resources.code.png"), DefaultEvent("ExecuteCode"), SRDescription("CodeActivityDescription")> _
    Public NotInheritable Class CodeActivity
        Inherits Activity
        ' Events
        <MergableProperty(False), SRDescription("UserCodeHandlerDescr"), SRCategory("Handlers")> _
        Public Custom Event ExecuteCode As EventHandler
            AddHandler(ByVal value As EventHandler)
                MyBase.AddHandler(CodeActivity.ExecuteCodeEvent, value)
            End AddHandler
            RemoveHandler(ByVal value As EventHandler)
                MyBase.RemoveHandler(CodeActivity.ExecuteCodeEvent, value)
            End RemoveHandler
        End Event

        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String)
            MyBase.New(name)
        End Sub

        Protected NotOverridable Overrides Function Execute(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            MyBase.RaiseEvent(CodeActivity.ExecuteCodeEvent, Me, EventArgs.Empty)
            Return ActivityExecutionStatus.Closed
        End Function


        ' Fields
        Public Shared ReadOnly ExecuteCodeEvent As DependencyProperty = DependencyProperty.Register("ExecuteCode", GetType(EventHandler), GetType(CodeActivity))

        ' Nested Types
        Private Class CodeActivityValidator
            Inherits ActivityValidator
            ' Methods
            Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
                Dim errors As New ValidationErrorCollection
                Dim activity As CodeActivity = [TryCast](obj,CodeActivity)
                If (activity Is Nothing) Then
                    Throw New InvalidOperationException
                End If
                If ((activity.GetInvocationList([Of] EventHandler)(CodeActivity.ExecuteCodeEvent).Length = 0) AndAlso (activity.GetBinding(CodeActivity.ExecuteCodeEvent) Is Nothing)) Then
                    Dim hashtable As Hashtable = [TryCast](activity.GetValue(WorkflowMarkupSerializer.EventsProperty),Hashtable)
                    If ((hashtable Is Nothing) OrElse (hashtable.Item("ExecuteCode") Is Nothing)) Then
                        errors.Add(ValidationError.GetNotSetValidationError("ExecuteCode"))
                    End If
                End If
                errors.AddRange(MyBase.Validate(manager, obj))
                Return errors
            End Function

        End Class
    End Class
End Namespace

